package StaffLogic

import (
	"CRM_rorm/logic"
	"CRM_rorm/model"
	"CRM_rorm/response"
	"CRM_rorm/service"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

var Role string

func StaffRegister(ctx *gin.Context) {
	var staff model.Staff
	err := ctx.Bind(&staff)
	staff.Delete_time = time.Unix(0, 0)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		fmt.Println(err)
		return
	}
	err = service.Svc.StaffProjectModel.Insert(staff)
	if err != nil {
		response.Fail(ctx, nil, "注册失败")
		return
	}
	token, err := logic.ReleaseToken(staff)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"code": 500, "msg": "系统异常"})
		return
	}
	// 返回结果
	response.Success(ctx, gin.H{"token": token}, "注册成功")
}

func StaffLogin(ctx *gin.Context) {
	var staff model.Staff
	err := ctx.Bind(&staff)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	staff1, err := service.Svc.StaffProjectModel.FindOneByTel(staff)
	if err != nil {
		response.Fail(ctx, nil, "登录失败,账号或密码错误")
		fmt.Println(err)
		return
	}
	if staff1.Password != staff.Password {
		response.Fail(ctx, nil, "登录失败,密码错误")
		return
	}
	token, err := logic.ReleaseToken(staff)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"code": 500, "msg": "系统异常"})
		return
	}
	response.Success(ctx, gin.H{
		"token": token,
	}, "登陆成功")
	Role = staff1.Role
}

func StaffDeleteSoft(ctx *gin.Context) {
	var staff model.Staff
	err := ctx.Bind(&staff)
	staff.Delete_time = time.Now()
	staff.DelState = 1
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	err = service.Svc.StaffProjectModel.DeleteSoft(staff)
	if err != nil {
		response.Fail(ctx, nil, "软删除失败")
		return
	}
	response.Success(ctx, nil, "软删除成功")
}

func StaffDelete(ctx *gin.Context) {
	var staff model.Staff
	err := ctx.Bind(&staff)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	err = service.Svc.StaffProjectModel.Delete(staff)
	if err != nil {
		response.Fail(ctx, nil, "删除失败")
		return
	}
	response.Success(ctx, nil, "删除成功")
}

func StaffUpdate(ctx *gin.Context) {
	var staff model.Staff
	err := ctx.Bind(&staff)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	err = service.Svc.StaffProjectModel.Update(staff)
	if err != nil {
		response.Fail(ctx, nil, "更改失败")
		return
	}
	response.Success(ctx, nil, "更改成功")
}

func StaffFindOneByStaffId(ctx *gin.Context) {
	var staff model.Staff
	err := ctx.Bind(&staff)

	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	staff, err = service.Svc.StaffProjectModel.FindOneByStaffId(staff)
	if err != nil {
		response.Fail(ctx, nil, "查询失败")
		return
	}
	response.Success(ctx, gin.H{
		"staff_id":         staff.Staff_id,
		"staff_project_id": staff.Staff_Project_id,
		"staff_name":       staff.Staff_name,
		"staff_telephone":  staff.Staff_Telephone,
		"role":             staff.Role,
	}, "查询成功")
}

func StaffFindOneByProjectId(ctx *gin.Context) {
	var staff model.Staff
	err := ctx.Bind(&staff)

	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	staff, err = service.Svc.StaffProjectModel.FindOneByProjectId(staff)
	if err != nil {
		response.Fail(ctx, nil, "查询失败")
		return
	}
	response.Success(ctx, gin.H{
		"staff_id":         staff.Staff_id,
		"staff_project_id": staff.Staff_Project_id,
		"staff_name":       staff.Staff_name,
		"staff_telephone":  staff.Staff_Telephone,
		"role":             staff.Role,
	}, "查询成功")
}
